package com.zrrd.yunchmall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.product.entity.ProductCategory;
import com.zrrd.yunchmall.product.service.IProductCategoryService;
import com.zrrd.yunchmall.user.entity.Admin;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品分类 前端控制器
 * </p>
 *
 * @author LYY
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/productCategory")
@Api(tags = "商品服务-商品类别模块")
public class ProductCategoryController {
    @Autowired
    private IProductCategoryService service;

    @ApiOperation("查找商品类别及其子类别")
    @GetMapping("/list/withChildren")
    public ResponseResult listWithChildren() {
        List<ProductCategory> list = service.listWithChildren();
        return new ResponseResult<>(200, "查询成功!", list);
    }

    @ApiOperation("查询商品分类列表")
    @GetMapping("/list/{parentId}")
    public ResponseResult productCategory(@ApiParam("主类商品名字") @PathVariable BigInteger parentId,
                                          @RequestParam int pageSize, @RequestParam int pageNum) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        IPage<ProductCategory> pageInfo = service.page(new Page<>(pageNum, pageSize), queryWrapper);
        return new ResponseResult<>(200, "查询成功!", pageInfo);
    }

    @GetMapping("/{id}")
    private ResponseResult result(@PathVariable("id") int id) {
        return new ResponseResult<>(200, "查询成功", service.getById(id));
    }

    @PostMapping("/update/{id}")
    private ResponseResult update(@PathVariable("id") int id, @RequestBody ProductCategory productCategory) {
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        return new ResponseResult<>(200, "查询成功", service.update(productCategory, updateWrapper));
    }

    @PostMapping("/create")
    private ResponseResult create(@RequestBody ProductCategory productCategory) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", productCategory.getName());
        if (service.count(queryWrapper) > 0) {
            return new ResponseResult<>(205, "已经有相同名字!");
        }
        return new ResponseResult<>(200, "查询成功", service.save(productCategory));
    }

    @PostMapping("/update/navStatus")
    private ResponseResult updateNavStatus(String ids, Integer navStatus) {
        for (String id : ids.split(",")) {
            UpdateWrapper updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("nav_status", navStatus);
            updateWrapper.in("id", Long.valueOf(id));
            service.update(updateWrapper);
        }
        return new ResponseResult<>(200, "修改成功！");
    }

    @PostMapping("/update/showStatus")
    private ResponseResult updateShowStatus(String ids, Integer showStatus) {
        for (String id : ids.split(",")) {
            UpdateWrapper updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("show_status", showStatus);
            updateWrapper.in("id", Long.valueOf(id));
            service.update(updateWrapper);
        }
        return new ResponseResult<>(200, "修改成功！");
    }
    @PostMapping("/{id}")
    private ResponseResult deleteById(@PathVariable("id") int id){
        return new ResponseResult<>(200,"删除成功!",service.removeById(id));
    }
}
